package com.huike.common;

import com.huike.common.enums.DataPeimi;
import com.huike.common.utils.DynamicEnumUtil;
import org.springframework.beans.factory.InitializingBean;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.Properties;
import java.util.logging.Logger;

/**
 * @
 */
public class DynamicEnumloadService implements InitializingBean {
    private static Logger logger = Logger.getLogger("");
    @Override
    public void afterPropertiesSet() {

        InputStream inputStream = DynamicEnumloadService.class.getResourceAsStream("/application.properties");
        //实例化Properties类
        Properties properties = new Properties();
        //调用load()方法加载properties文件，load里面传入InputSteam类型的参数或者Reader类型的参数
        try {
            properties.load(new InputStreamReader(inputStream,"UTF-8"));
        } catch (IOException e) {
            logger.info("TestEnum 动态枚举加载失败......");
            e.printStackTrace();
        }

        Enumeration em = properties.propertyNames();
        while (em.hasMoreElements()) {
            String key = (String) em.nextElement();
            String value = properties.getProperty(key);
            String[] split = value.split("\\|");
            addTestEnum(key, split);
        }

        logger.info("TestEnum 动态枚举加载成功......");
        for (DataPeimi tataPeimi : DataPeimi.values()) {
            logger.info(tataPeimi.toString());
        }

    }

    /**
     * 新增 TestEnum 枚举项
     *
     * @param enumName 枚举 名称
     * @param objects 枚举项
     */
    private static void addTestEnum(String enumName, Object[] objects) {
        DynamicEnumUtil.addEnum(DataPeimi.class, enumName, new Class<?>[]
                {String.class, String.class}, objects);
    }
}
